package com.example.mybatisplus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.pojo.Login;
import com.example.mybatisplus.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    LoginService loginService;
    // 根据ID查询用户
    @GetMapping("/test/{id}")
    public Login getById(@PathVariable Long id) {
        Login login = loginService.getById(id);
        log.info("查询结果：{}", login);
        return login;
    }

    @GetMapping("/test/list")
    public List<Login> getById() {
        List<Login> l = loginService.list();
        log.info("查询结果：{}", l);
        return l;
    }
    //分页查询
    @GetMapping("/test/list/page")
    public Page<Login> getLoginPage(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        Page<Login> page = loginService.getLoginPage(pageNum, pageSize);
        log.info("分页查询结果：{}", page.getRecords());
        return page;
    }

    @PostMapping ("/test/delete")
    public boolean deleteById(@RequestParam String id) {
       loginService.removeById(id);
        return true;
    }

    @PostMapping ("/test/add")
    public boolean add(@RequestBody Login login) {
        loginService.save(login);
        return true;
    }

    @PostMapping ("/test/update")
    public boolean update(@RequestBody Login login) {
        loginService.updateById(login);
        return true;
    }

}
